package kr.inani.android.view;

import kr.inani.android.BookDetailActivity;
import kr.inani.android.MainActivity;
import kr.inani.android.search.BookItem;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class BookView extends ImageView implements OnClickListener
{
	public static final int	BOOK_IMAGE_WIDTH			= 100;
	public static final int	BOOK_IMAGE_HEIGHT			= 120;
	
	protected BookItem bookItem;
	private View.OnLongClickListener longClickListener;
	private CustomHorizontalScrollView hScrollView;
	
//	public BookView(Context context, AttributeSet attrs, int defStyle)
//	{
//		super(context, attrs, defStyle);
//		initLayoutParam(context);
//	}
//
//	public BookView(Context context, AttributeSet attrs)
//	{
//		super(context, attrs);
//		initLayoutParam(context);
//	}
//	
	public BookView(Context context)
	{
		super(context);
		initLayoutParam(context);
	}

	public BookView(Context context, View.OnLongClickListener longClickListener)
	{
		super(context);
		this.longClickListener = longClickListener;
		initLayoutParam(context);
	}
	
	
	@Override
	public void onClick(View v)
	{
		if(bookItem!=null) {
			Intent intent = new Intent(getContext(), BookDetailActivity.class);
			intent.putExtra(BookItem.BOOK_ITEM_STRING, bookItem);
			intent.putExtra("enableDelete", true);
			((MainActivity)getContext()).startActivityForResult(intent, MainActivity.REQUEST_CODE_FOR_DELETE_BOOK);
		}
	}
	
	public void setBookItem(BookItem bookItem)
	{
		this.bookItem = bookItem;
		if(bookItem!=null) {
			setImageBitmap(bookItem.getBitmap());
		} else {
			setImageBitmap(null);
		}
	}
	
	public BookItem getBookItem()
	{
		return bookItem;
	}

	public void setHScrollView(CustomHorizontalScrollView hScrollView) {
		this.hScrollView = hScrollView;
	}

	public int getScrolledX()
	{
		if(hScrollView!=null) {
			return hScrollView.getScrolledX();
		}
		return 0;
	}
	
	
	////////////////////// private ///////////////////////////////////

	private void initLayoutParam(Context context)
	{
		LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(BOOK_IMAGE_WIDTH, BOOK_IMAGE_HEIGHT);
		layoutParam.setMargins(10, 60, 10, 10);
		setLayoutParams(layoutParam);
		setOnClickListener(this);
		setOnLongClickListener(longClickListener);
	}

	
}
